package com.victor.netty.sixexample;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

/**
 * Created by DJJ on 2019/6/2.
 */
public class TestClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int random = new Random().nextInt(3);
        MyDataInfo.MyMessage myMessage=null;
        if (0==random){
            myMessage= MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.PerSonType)
                    .setPerson(MyDataInfo.Person.newBuilder().setAddress("广州").setAge(55).setName("张三").build())
                    .build();
        }else if (1==random){
            myMessage= MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.DogType)
            .setDog(MyDataInfo.Dog.newBuilder().setAge(2).setName("wangcai").build())
            .build();
        }else {
            myMessage= MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.CatType)
                    .setCat(MyDataInfo.Cat.newBuilder().setCity("福建").setName("花花").build())
                    .build();
        }
        ctx.channel().writeAndFlush(myMessage);
    }
}
